#!/bin/bash

INPUT_INFILE=$1
OUTPUT_FILE=$2
#echo $OUTPUT_FILE
for DIR in "BW-"$1 #"ubenchflit-"$1 #flit-64 flit-128 flit-256 flit-512 "
do
 for FILE in $DIR/*.txt
 do
    #echo $FILE
    NUM_INSTR=$(grep Million $FILE | tail -n 1 | cut -d" " -f4)
    IPC=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f4 | cut -d"i" -f1)
    L1MPKI=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f6 | cut -d"l" -f1)
    L2MPKI=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f7 | cut -d"l" -f1)
    LAT=$(grep "Avg. Message Latency" $FILE | tail -n 1 | cut -d":" -f2)
    POW_NET=$(grep "Avg. Power consumption"  $FILE | tail -n 1 | cut -d":" -f2)
    MLP=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | cut -d":" -f13 | cut -d" " -f1 | awk '{sum+=$1} END { print "MLP:", sum/NR}')
    SLACK=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | cut -d":" -f14 | cut -d" " -f1 | awk '{sum+=$1} END { print "SLACK:", sum/NR}')
    AVG_COP=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | cut -d":" -f15 | cut -d" " -f1 | awk '{sum+=$1} END { print "COP:", sum/NR}')
    AVG_COMP=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | cut -d":" -f16 | cut -d" " -f1 | awk '{sum+=$1} END { print "COMP:", sum/NR}')
    NUM_NET_PH=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f17 | cut -d" " -f1 | awk '{sum4+=$1} END { print "NUM_NET_PH:", sum4/NR}')
    TOT_NET_LN=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f18 | cut -d" " -f1 | awk '{sum5+=$1} END { print "TOT_NET_LN:", sum5/NR}')
    TOT_NET_HT=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f19 | cut -d" " -f1 | awk '{sum5+=$1} END { print "TOT_NET_HT:", sum5/NR}')
    TOT_COMP_LN=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f20 | cut -d" " -f1 | awk '{sum5+=$1} END { print "TOT_COMP_LN:", sum5/NR}')
    TOT_AVG_HT_PER_NET_LN=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f21 | cut -d" " -f1 | awk '{sum5+=$1} END { print "TOT_AVG_HT_PER_NET_LN:", sum5/NR}')
    RUNNING_AVG_HT_PER_NET_LN=$(grep -v "procID(nop)" $FILE | grep "procID" | grep 35 | tail -n 1 | cut -d":" -f23 | cut -d" " -f1 | awk '{sum5+=$1} END { print "RUNNING_AVG_HT_PER_NET_LN:", sum5/NR}')
    echo $FILE $NUM_INSTR $IPC $L1MPKI $L2MPKI $LAT $POW_NET $MLP $SLACK $AVG_COP $AVG_COMP $NUM_NET_PH $TOT_NET_LN $TOT_NET_HT $TOT_COMP_LN $TOT_AVG_HT_PER_NET_LN $RUNNING_AVG_HT_PER_NET_LN >> $OUTPUT_FILE
 done
done
